<template>
	<view class="content">
		<image src="https://xb-street.oss-cn-shanghai.aliyuncs.com/xb-doctor/static/images/chahua@2x.png" mode="widthFix" style="width:100%"></image>
		<view class="title">相伴医路申请获得以下权限</view>
		<view class="desc">获得您的公开信息（昵称、头像等）</view>
		
		<view class="btn" @click="auth()">
			确认授权
		</view>
	</view>
	
</template>  

<script>
	export default {
		data() {
			return {
				sex:'',
				code:'',
				nickName:'',
				avatar:'',
				sessionKey:'',
				shopName:'',
				accessId:''
			}
		},
		onLoad() {
			// this.shopName = uni.getStorageSync('shopName')
			// this.accessId = uni.getStorageSync('accessId')
			// #ifdef MP-WEIXIN
			uni.login({
				success: (res) => {
					if (res.errMsg == "login:ok") {
						this.code = res.code;
					} else {
						uni.showToast({
							title: '系统异常，请联系管理员!'
						})
					}
				}
			})
			// #endif
		
		},
		onUnload() {
			console.log('监听返回按钮')
			uni.switchTab({
				url:'../index/index'
			})
		},
		methods: {
			auth(){
				//TODO::模拟登录，实现登录接口
				// uni.setStorageSync('token', 'xxxxxxxxxxxxx');
				// uni.setStorageSync('hadLogin', 1);
				// uni.switchTab({
				// 	url:'index'
				// })
				let _this = this
				uni.showLoading({
				    title: '加载中'
				});
				uni.getUserProfile({
					desc: '用户信息获取',
					success: res => {
						//存储信息
						let resUserInfo = JSON.parse(res.rawData)
						let provider = {
							userId: '',
							image: resUserInfo.avatarUrl,
							name: resUserInfo.nickName,
							gender: resUserInfo.gender,
							country: resUserInfo.country,
							province: resUserInfo.province,
							city: resUserInfo.city,
							code: _this.code
						}
						_this.sex = resUserInfo.gender
						_this.nickName = resUserInfo.nickName
						_this.avatar = resUserInfo.avatarUrl
						//赋值用户信息
						_this.$api_common.getSessionKey({
							code: _this.code,
							rawData: res.rawData,
							signature: res.signature,
							encryptedData: res.encryptedData,
							iv: res.iv,
							accessId:'DAYyeQdPaX869zoUpcKl0Zu2G',
						}).then((res) => {
							console.log(res)
							if (res.data.success) {
								//存储用户信息
								provider.userId = res.data.dataMap.userId
								// _this.login(provider)
								_this.sessionKey = res.data.dataMap.sessionKey
								// _this.isAuthInfo = !_this.isAuthInfo
								uni.showToast({
									icon: 'none',
									title: '授权成功',
									duration: 1500
								})
								let parameter={
									sessionKey:_this.sessionKey,
									sex:_this.sex,
									avatar:_this.avatar,
									nickName:_this.nickName,
								}
								_this.$store.state.sessionUserInfo = parameter
								uni.navigateTo({
									url:'login'
								})
								// _this.active=2
							} else {
								console.log("接口异常")
							}
							uni.hideLoading();
						
						})
					},
					fail(res) {
						uni.hideLoading();
						console.log('失败了')
					}
				})
			}
		}
	}
</script>

<style>
  .content{
	  display: flex;
	  justify-content: center;
	  align-items: center;
	  flex-direction: column;
	  padding:20% 20px;
	  box-sizing: border-box;
  }
  .title{font-weight: bold;margin-top: 20px;}
  .desc{color:#999999}
  .btn{
  	   background-color: #FF9338;
  	   border-radius:50px;
	   line-height: 100rpx;
  	   width: 90%;
	   height: 100rpx;
  	   font-size: 32rpx;
  	   margin-top:20px;
  	   color:white;text-align: center;
  }
</style>

